package com.yfp.client.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.yfp.client.domain.DeviceCurrentComboStock;
import com.yfp.client.domain.ShoppingCarts;
import com.yfp.client.domain.dto.ShoppingCartsDto;
import com.yfp.client.domain.vo.ShoppingCartsVo;
import com.yfp.client.mapper.ShoppingCartsMapper;
import com.yfp.client.service.DeviceCurrentComboStockService;
import com.yfp.client.service.ShoppingCartsService;
import com.yfp.client.util.SecurityUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 *
 * @description 针对表【shopping_carts(购物车表)】的数据库操作Service实现
 * @createDate 2024-05-24 17:20:15
 */
@Service
public class ShoppingCartsServiceImpl implements ShoppingCartsService {

    @Resource
    private ShoppingCartsMapper shoppingCartsMapper;
    @Resource
    private DeviceCurrentComboStockService deviceCurrentComboStockService;

    @Override
    public List<ShoppingCartsVo> findClientShoppingCarts(Long machineId) {
        String clientId = SecurityUtils.getClientId();
        // Long clientId = 1L;
        List<ShoppingCartsVo> clientShoppingCarts = shoppingCartsMapper.findClientShoppingCarts(clientId, machineId);
        // 获取套餐库存
        List<DeviceCurrentComboStock> deviceCurrentComboStocks = deviceCurrentComboStockService.getByMachineId(machineId);
        Map<String, Integer> deviceStockMapByComboId = deviceCurrentComboStocks.stream().collect(Collectors.toMap(DeviceCurrentComboStock::getComboId, DeviceCurrentComboStock::getComboNum, (k1, k2) -> k1 + k2));
        clientShoppingCarts.forEach(shoppingCartsVo -> {
            Integer comboNum = deviceStockMapByComboId.get(shoppingCartsVo.getPackageId());
            shoppingCartsVo.setStockQuantity(comboNum);
        });
        return clientShoppingCarts;
    }

    @Override
    public void save(ShoppingCartsDto shoppingCartsDto) {
        ShoppingCarts shoppingCarts = BeanUtil.toBean(shoppingCartsDto, ShoppingCarts.class);
        shoppingCarts.setClientId(SecurityUtils.getClientId());
        shoppingCartsMapper.insert(shoppingCarts);
    }

    @Override
    public void deleteShoppingCartsByIds(Long[] idList) {
        shoppingCartsMapper.deleteShoppingCartsByIds(idList);
    }

    @Override
    public void updateShoppingCarts(ShoppingCartsDto shoppingCartsDto) {
        // 如果数量等于0 直接删除
        if (shoppingCartsDto.getQuantity() <= 0) {
            shoppingCartsMapper.deleteShoppingCartsById(shoppingCartsDto.getId());
        } else {
            if (Objects.isNull(shoppingCartsDto.getId())) {
                this.save(shoppingCartsDto);
            } else {
                shoppingCartsMapper.updateShoppingCarts(shoppingCartsDto);
            }
        }
    }

    /**
     * 根据用户id和设备id删除购物车
     *
     * @param clientId  用户id
     * @param machineId 设备id
     */
    @Override
    public void deleteByUserIdAndMachineId(String clientId, String machineId) {
        shoppingCartsMapper.deleteByUserIdAndMachineId(clientId, machineId);
    }

    /**
     * 根据购物车id删除购物车
     *
     * @param id 购物车id
     */
    @Override
    public void deleteShoppingCartsById(Long id) {
        shoppingCartsMapper.deleteShoppingCartsById(id);
    }

}




